package com.shuoyue.ycgk.ui.recruitment.job;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.JobInfo;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JobListActivity extends BaseMvpActivity<JobContract.Presenter>
        implements JobContract.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    @BindView(R.id.edit)
    EditText edit;
    @BindView(R.id.close)
    ImageView close;


    JobAdapter jobAdapter;
    ListWithPage listWithPage;
    int modelId;
    String title;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jobs_list;
    }

    @Override
    protected void initView() {
        pageTitle.setText("职位");
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));

        jobAdapter = new JobAdapter(null);
        recyclerView.setAdapter(jobAdapter);
        refreshlayout.setEnableLoadMore(false);

        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getJobs(modelId == -1 ? null : modelId, null, 1, title);
            refreshLayout.finishRefresh(1000);
        });
        refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getJobs(modelId == -1 ? null : modelId, null, listWithPage.getCurrent() + 1, title);
            refreshLayout.finishLoadMore(1000);
        });
        registEmptyView(jobAdapter);
        jobAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            JobInfo jobInfo = (JobInfo) adapter.getItem(position);
            startActivity(new Intent(this, JobDetailActivity.class)
                    .putExtra("id", jobInfo.getId()));
        });
        edit.setOnEditorActionListener((v, actionId, event) -> {
            title = edit.getText().toString();
            mPresenter.getJobs(modelId == -1 ? null : modelId, null, 1, title);
            return false;
        });
    }

    @Override
    public void initData() {
        super.initData();
        modelId = getIntent().getIntExtra("modelId", -1);
        mPresenter = new JobContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getJobs(modelId == -1 ? null : modelId, null, 1, null);
    }


    @Override
    public void setJobList(ListWithPage<JobInfo> list) {
        pageTitle.setText("新职位(" + list.getTotal() + ")");
        listWithPage = list;
        if (list.getCurrent() == 1) {
            jobAdapter.resetData(list.getRecords());
        } else {
            jobAdapter.addData(list.getRecords());
        }
        refreshlayout.setEnableLoadMore(list.getCurrent() != list.getPages() && list.getPages() > 0);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }


    @OnClick({R.id.back, R.id.close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.close:
                this.title = null;
                edit.setText("");
                mPresenter.getJobs(modelId == -1 ? null : modelId, null, 1, null);

                break;
        }
    }
}
